local focuses = {}
local nestings = {}
function isFocused(cid)
	for i, v in pairs(focuses) do
		if(v == cid) then
			return true
		end
	end
	return false
end

function getNesting(cid)
	return nestings[cid]
end

function setNesting(cid, i)
	nestings[cid] = i
end

function addFocus(cid)
	if(not isFocused(cid)) then
		table.insert(focuses, cid)
	end
end

function removeFocus(cid)
	nestings[cid] = nil
	for i, v in pairs(focuses) do
		if(v == cid) then
			table.remove(focuses, i)
			break
		end
	end
end

function _(msg, text)
	if type(text) == 'string' then
		return string.find( msg, string.lower(text), 1, true )
	else
		for index,textnode in pairs(text) do
			if string.find( msg, string.lower(textnode), 1, true ) then
				return true
			end
		end
		return false
	end
end

function onCreatureDisappear(cid)
	if isFocused(cid) then
		selfSay( 'How rude!' )
		if isPlayer(cid) == TRUE then
			closeShopWindow(cid)
			removeFocus(cid)
		end
	end
end

function onCreatureMove(cid,frompos,topos)
		if isFocused(cid) then
		    local distance = getDistanceToCreature(cid) or -1
			if((distance > 3) or (distance == -1)) then
				selfSay( "How rude!" )
				closeShopWindow(cid)
				removeFocus(cid)
			end
		end
end

function onPlayerCloseChannel(cid)
	selfSay( "How rude!" )
	closeShopWindow(cid)
	removeFocus(cid)
end

function onCreatureSay(cid, type, msg)
	msg = string.lower(msg)
	
	if _(msg, {'hi','hello','hiho'}) and not isFocused(cid) then
		selfSay( 'Hello, ' .. getCreatureName(cid) .. '! I\'m selling {citizen} {addon}.', cid )
		addFocus(cid)
		nestings[cid] = 0
	end
	
	if isFocused(cid) and type == 4 then
		if getNesting(cid) == 0 then
			if _(msg, 'job') then
				selfSay( 'I\'m addoner.', cid )
			elseif _(msg, 'citizen addon') then
				selfSay( 'I\'m offering citizen {backpack} and citizen {hat}.', cid )
			elseif _(msg, 'backpack') then
				if getPlayerStorageValue(cid, 7000) == -1 then
					selfSay('To obtain citizen backpack, bring me 100 minotaur leathers. After that, ask me again.', cid)
					setPlayerStorageValue(cid,7000,1)
				elseif getPlayerStorageValue(cid,7000) == 1 then
					selfSay('Do you have 100 minotaur leathers with you?', cid)
					setNesting(cid,1)
				elseif getPlayerStorageValue(cid,7000) == 2 then
					selfSay('You already have this addon.', cid)
				end
			elseif _(msg, 'hat') then
				if getPlayerStorageValue(cid, 7001) == -1 then
					selfSay('To obtain citizen hat, bring me 100 chicken feathers, 50 honeycombs and a legion helmet. After that, ask me again.', cid)
					setPlayerStorageValue(cid,7001,1)
				elseif getPlayerStorageValue(cid, 7001) == 1 then
					selfSay('Do you have 100 chicken feathers, 50 honeycombs and a legion helmet with you?', cid)
					setNesting(cid,2)
				elseif getPlayerStorageValue(cid,7001) == 2 then
					selfSay('You already have this addon.', cid)
				end
			elseif _(msg, {'bye','farewell'}) then
				selfSay('Good bye, ' .. getCreatureName(cid) .. '!', cid)
				removeFocus(cid)
			end
		elseif getNesting(cid) == 1 then
			if _(msg, 'yes') then
				local leathers = getPlayerItemCount(cid, 5878)
				if leathers >= 100 then
					doPlayerRemoveItem(cid, 5878, 100)
					selfSay('Fine. You now have citizen backpack.', cid)
					looktype = 128
					if getPlayerSex(cid) == FEMALE then looktype = 136; end
					if canPlayerWearOutfit(cid, looktype, 2) == TRUE then
						doPlayerAddOutfit(cid, looktype, 3)
					else
						doPlayerAddOutfit(cid, looktype, 1)
					end	
					setPlayerStorageValue(cid,7000,2)
					setNesting(cid,0)
				else
					selfSay('Come back when you\'ll have 100 leathers.', cid)
					setNesting(cid,0)
				end
			else 
				selfSay('Maybe next time.',cid)
				setNesting(cid,0)
			end
		elseif getNesting(cid) == 2 then
			if _(msg, 'yes') then
				local feathers = getPlayerItemCount(cid, 5890)
				local honeycombs = getPlayerItemCount(cid, 5902)
				local helmet = getPlayerItemCount(cid, 2480)
				if feathers >= 100 and honeycombs >= 50 and helmet >= 1 then
					doPlayerRemoveItem(cid, 5890, 100)
					doPlayerRemoveItem(cid, 5902, 50)
					doPlayerRemoveItem(cid, 2480, 1)
					selfSay('Fine. You now have citizen hat.', cid)
					looktype = 128
					if getPlayerSex(cid) == FEMALE then looktype = 136; end
					if canPlayerWearOutfit(cid, looktype, 1) == TRUE then
						doPlayerAddOutfit(cid, looktype, 3)
					else
						doPlayerAddOutfit(cid, looktype, 2)
					end
					setPlayerStorageValue(cid,7001,2)
					setNesting(cid,0)
				else
					selfSay('Come back when you\'ll have 100 chicken feathers, 50 honeycombs and 1 legion helmet.', cid)
					setNesting(cid,0)
				end
			else
				selfSay('Maybe next time.',cid)
				setNesting(cid,0)
			end
		end
	end
end
